<?php

namespace Model;

/**
 * Class Page
 * @property-read \Lib\Functions $fns
 * @package Model
 */
class Page extends Model {
    const PAGE_SAVE_DIR = 'pages';

    CONST PT_LIST = 'LIST';
    CONST PT_TEXT = 'TEXT';
    CONST PT_OBJECT = 'OBJECT';
    CONST PT_TEXTAREA = 'TEXTAREA';
    CONST PT_RICHTEXT = 'RICHTEXT';
    CONST PT_SELECT = 'SELECT';
    CONST PT_SWITCH = 'SWITCH';
    CONST PT_RADIO = 'RADIO';
    CONST PT_CHECKBOX = 'CHECKBOX';
    CONST PT_IMG = 'IMG';

    CONST IMG_SIZE_ORIGINAL = 'ORIGINAL';
    CONST IMG_SIZE_ADAPT = 'ADAPT';
    CONST IMG_SIZE_CUSTOM = 'CUSTOM';
    
    public function getInfo($page) {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $file = "$pageDirPath/$page.page.json";
        
        if(!file_exists($file)) {
            return [];
        }
        
        $json = file_get_contents($file);
        $data = json_decode($json, true);
        $data['last_updated_at'] = filemtime($file);
        $data['id'] = $page;
        return $data;
    }
    
    public function getTemplatePath($template) {
        $pageDir = $this->core->rootPath('core/pages');
        return "$pageDir/$template.phtml";
    }
    
    public function getTemplateInfo($template) {
        $templateFile = $this->getTemplatePath($template);
        
        if(!file_exists($templateFile)) {
            return [];
        }
        
        $_get_template_info = true;
        $data = include($templateFile);
        unset($_get_template_info);
        return $data;
    }
    
    public function exists($page) {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $file = "$pageDirPath/$page.page.json";

        return file_exists($file);
    }
    
    public function save($page, $data) {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $file = "$pageDirPath/$page.page.json";
        
        if(!is_dir($pageDirPath)) {
            mkdir($pageDirPath, 0777, true);
        }
        
        file_put_contents($file, json_encode($data));
    }
    
    public function remove($page) {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $file = "$pageDirPath/$page.page.json";
        
        if(file_exists($file)) {
            unlink($file);
        }
    }
    
    public function getTemplateVal($template, $key) {
        static $templates = null;
        
        if($templates === null) {
            $templates = $this->getTemplates();
            $newTemplates = [];
            
            foreach ($templates as $temp) {
                $newTemplates [$temp['key']] = $temp;
            }
            
            $templates = $newTemplates;
        }
        
        return $templates[$template][$key];
    }
    
    public function getTemplates() {
        $pageDir = $this->core->rootPath('core/pages');

        if(!file_exists($pageDir)) {
            return [];
        }

        $templateFiles = glob($pageDir . '/*.phtml');

        if(empty($templateFiles)) {
            return [];
        }

        $templates = [];
        $_get_template_info = true;

        foreach ($templateFiles as $file) {
            $info = include $file;
            $templates []= $info;
        }

        unset($_get_template_info);
        return $templates;
    }
    
    public function getPages() {
        $pageDirPath = $this->core->storagePath(self::PAGE_SAVE_DIR);
        $pages = glob("$pageDirPath/*.page.json");

        $pageInfos = [];

        foreach ($pages as $pageFile) {
            $id = preg_replace('#\.page\.json$#i', '', basename($pageFile));
            $pageInfos []= $this->getInfo($id);
        }

        return $pageInfos;
    }
}